package main.java.com.lx.tools.generator

import com.intellij.openapi.project.Project
import main.java.com.lx.tools.utils.PlatformUtil
import java.io.File

class MyBatisGenConst {

    /**
     * 伴生对象。companion中的内容，可以通过类直接访问。
     */
    companion object {
        // 包名
        var sDaoPackage = ""
        var sServicePackage = ""
        var sTablePrefix = ""

        // 输出目录
        private var sDaoPackageDir = ""
        var sMapperXmlDir = ""
        var sDoJavaDir = ""
        var sMapperJavaDir = ""
        var sServiceJavaDir = ""
        var sServiceImplJavaDir = ""
        var sHasBigDecimal = false

        //模板路径
        val DO_TEMPLATE = "/template/do.txt"
        val SQLMAP_TEMPLATE = "/template/sqlmap.txt"
        val MAPPER_TEMPLATE = "/template/mapper.txt"
        val SERVICE_TEMPLATE = "/template/service.txt"
        val SERVICE_IMPL_TEMPLATE = "/template/serviceImpl.txt"
        val MAPPER_SUFFIX = "Mapper"
        val MAPPER_XML_SUFFIX = "-mapper"
        val SERVICE_SUFFIX = "Service"
        val SERVICE_IMPL_SUFFIX = "ServiceImpl"
        val DO_SUFFIX = ""

        val COMMON_COLUMN_STR = "created_date,updated_date,"

        // jdbc result set metadata column name
        val RSMD_COLUMN_NAME = "rsmdColumnName"
        val RSMD_COLUMN_CLASS_NAME = "columnClassName"
        val RSMD_COLUMN_TYPE_NAME = "columnTypeName"
        val RSMD_COLUMN_PRECISION = "Precision"
        val RSMD_COLUMN_SCALE = "Scale"
        val RSMD_COLUMN_PRIMARY_KEY = "PrimaryKey"
        val RSMD_COLUMN_REMARKS = "Remarks"
        val COLUMN_NAME = "COLUMN_NAME"
        val HAS_BIG_DECIMAL = "hasBigDecimal"

        // velocity param
        val VP_LIST = "list"
        val VP_DO_SUFFIX = "doSuffix"
        val VP_MAPPER_SUFFIX = "mapperSuffix"
        val VP_SERVICE_SUFFIX = "serviceSuffix"

        val VP_SERVICE_IMPL_SUFFIX = "serviceImplSuffix"
        val VP_CLASS_NAME = "className"
        val VP_MAPPER_PROPERTY_NAME = "mapperPropertyName"
        val VP_DAO_PACKAGE = "daoPackage"
        val VP_DO_PACKAGE = "doPackage"
        val VP_MAPPER_PACKAGE = "mapperPackage"
        val VP_SERVICE_PACKAGE = "servicePackage"
        val VP_SERVICE_IMPL_PACKAGE = "serviceImplPackage"
        val VP_JAVA_TYPE = "javaType"
        val VP_PROP_NAME = "propName"
        val VP_GET_METHOD = "getMethod"
        val VP_SET_METHOD = "setMethod"
        val VP_COLUMN_NAME = "columnName"
        val VP_COLUMN_REMARKS = "columnRemarks"
        val VP_TABLE_NAME = "tableName"
        val VP_JDBC_TYPE = "jdbcType"
        val VP_COLS = "cols"
        val VP_COLS_WITHOUT_COMMON_COLUMNS = "colsWithoutCommColumns"
        val VP_SERIAL_VERSION_UID = "serialVersionUID"

        val VP_SERIAL_VERSION_UID2 = "serialVersionUID2"
        val VP_PRIMARY_KEY = "primaryKey"
        val VP_PROP_PRIMARY_KEY = "propPrimaryKey"

        //路径初始化
        fun initCodeConfig(project: Project?): Boolean {
            PlatformUtil.getData(MyConstant.DAO_PACKAGE, "").let {
                if (it.isBlank()) {
                    sDaoPackage = ""
                    return false
                } else {
                    sDaoPackage = it
                }
            }

            PlatformUtil.getData(MyConstant.SERVICE_PACKAGE, "").let {
                if (it.isBlank()) {
                    sServicePackage = ""
                    return false
                } else {
                    sServicePackage = it
                }
            }

            PlatformUtil.getData(MyConstant.TABLE_PREFIX, "").let {
                sTablePrefix = if (it.isBlank()) {
                    ""
                } else {
                    it
                }
            }
            setConfig(sDaoPackage,
                    sServicePackage,
                    sTablePrefix,
                    project)
            return true
        }

        //路径
        private fun setConfig(
                daoPackage: String,
                servicePackage: String,
                tablePrefix: String,
                project: Project?
        ) {
            if (null == project) {
                return
            }
            sDaoPackage = daoPackage
            sServicePackage = servicePackage
            sTablePrefix = tablePrefix
            sDaoPackageDir = project.baseDir.path + File.separator + "generator"
            val servicePath = project.baseDir.path + File.separator + "generator"
            sMapperXmlDir = sDaoPackageDir + "/src/main/resources/mapper/"
            val sBaseDir = sDaoPackageDir + "/src/main/java/" + getFilePathFromPackage(sDaoPackage)
            sDoJavaDir = sBaseDir + "/model"
            sMapperJavaDir = sBaseDir + "/mapper"
            sServiceJavaDir = servicePath + "/src/main/java/" + getFilePathFromPackage(sServicePackage) + "/service"
            sServiceImplJavaDir = sServiceJavaDir + "/impl"
        }

        private fun getFilePathFromPackage(packageName: String): String {
            return packageName.replace(".", "/")
        }

        fun clearConfig() {
            sDaoPackage = ""
            sServicePackage = ""
            sTablePrefix = ""
            sMapperXmlDir = ""
            sDoJavaDir = ""
            sMapperJavaDir = ""
            sServiceJavaDir = ""
            sServiceImplJavaDir = ""
        }

    }
}